package com.apes.fico.finance.service;
import com.apes.fico.finance.model.costcollect.ProportionServeType;
import com.apes.fico.finance.respository.ProportionServeTypeRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.product.repository.ServeTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static java.lang.Double.*;

/**
 * @author: cyy
 * @create: 2022-12-13 14:22
 */
@Service("proportionServeTypeService")
public class ProportionServeTypeService extends DomainService {
    @Autowired
    private DeptRepository deptRepository;
    @Autowired
    private ServeTypeRepository serveTypeRepository;
    @Autowired
    private ProportionServeTypeRepository proportionServeTypeRepository;

    //计算服务类型比例
    private void calculation(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        Date lastMonth = c.getTime();
        String months = DateUtil.format(lastMonth, "MM");
        String years = DateUtil.format(lastMonth, "YYYY");
        //按部门与服务类型分类
        List<Map<String, Object>> sgTc = proportionServeTypeRepository.findSgTc(months, years);
        //获取所有部门
        List<Map<String, Object>> deptIdList = proportionServeTypeRepository.findDeptId(months, years);
        deptIdList.forEach(deptId -> {
            //记录循环次数
            AtomicInteger count = new AtomicInteger();
            //记录比例
            AtomicReference<Double> num = new AtomicReference<>((double) 0);
            //总施工提成
            double sum = sgTc.stream().filter(map -> map.get("deptId").equals(deptId.get("deptId"))).mapToDouble(map -> parseDouble(map.get("SGTC").toString())).sum();
            sgTc.stream().filter(map -> map.get("deptId").equals(deptId.get("deptId"))).forEach(map -> {
                count.getAndIncrement();
                ProportionServeType proportionServeType = new ProportionServeType();
                proportionServeType.setDept(deptRepository.findOne(map.get("deptId").toString()));
                proportionServeType.setYearMonth(lastMonth);
                proportionServeType.setAmt(parseDouble(map.get("SGTC").toString()));
                proportionServeType.setServeType(serveTypeRepository.findOne(map.get("serveTypeId").toString()));
                //最后一条不使用除法，使用减法计算比例
                if(count.get() == sgTc.stream().filter(map1 -> map1.get("deptId").equals(deptId.get("deptId"))).count()){
                    proportionServeType.setProportion(BigDecimal.valueOf(1).subtract(BigDecimal.valueOf(num.get())).doubleValue());
                } else {
                    double proportion = Arith.round(parseDouble(map.get("SGTC").toString()) / sum,2);
                    proportionServeType.setProportion(proportion);
                    num.updateAndGet(v1 -> (v1 + proportion));
                }
                proportionServeTypeRepository.saveAndFlush(proportionServeType);
            });
        });
    }






















}
